import { reqSpaceManager, reqProductModelPage, reqOrgList } from "@api/api.js";
export default {
  data() {
    return {
      timeArr: [],
      orgName: "",
      selectedValue: "",

      // 建筑物
      sysInstanceIndex2: 1,
      sysInstanceTotalPages2: 10,
      loading2: false,
      buildings: [],
      buildingsName2: "",
      buildingsName: "",
      hasSearch: false,

      //   楼层
      sysInstanceIndex3: 1,
      sysInstanceTotalPages3: 10,
      loading3: false,
      floors: [],
      floorsName: "",

      // 安装点
      spaces: [],
      spacesName: "",
      spacesName2: "",
      sysInstanceIndex4: 1,
      sysInstanceTotalPages4: 10,
      loading4: false,
    };
  },
  mounted() {
    if (
      this.user.roleName == "INSTERALL_USER" ||
      this.user.roleName == "UNIT_ADMIN"
    ) {
      this.getOrgList();
    }
  },
  methods: {
    // 搜索
    async searchClick1() {
      this.page = 1;
      this.limit = 10;
      this.showPopover = false;
      await this.getDataList();
      this.clearSelections();
    },
    // 高级搜索重置
    async resetClick() {
      this.page = 1;
      this.limit = 10;
      this.searchForm.facilityName = "";
      this.searchForm.fireOsTypes = [];
      this.searchForm.facilityTypes = [];
      this.searchForm.status = [];
      this.searchForm.orgId = "";
      this.searchForm.spaceId = "";
      this.searchForm.buildingId = "";
      this.searchForm.floorId = "";
      this.timeArr = [];
      this.buildings = [];
      this.selectedValue = "";
      this.buildingsName = "";
      this.floorsName = "";
      // this.targetId = this.spacesManage.organizationInfo.orgId;
      if (this.$refs.refTreeSearch) {
        this.$refs.refTreeSearch.onclose();
      }
      this.searchForm.buildingId = ""; // 清空建筑物
      this.page = 1;
      this.limit = 10;
      await this.getDataList();
      this.clearSelections();

      if (
        this.user.roleName != "INSTERALL_USER" &&
        this.user.roleName != "UNIT_ADMIN"
      ) {
        this.orgList = [];
      }
    },
    changeunitId(e) {
      this.searchForm.unitId = e;
      this.handleChange(2);
    },
    // 远程搜索
    async remoteMethod(query) {
      this.selectedValue = query; //保留输入的值
      this.orgName = query;
      this.sysInstanceIndex = 1;
      this.sysInstanceTotalPages = 10;
      this.orgList = [];
      this.getOrgList();
    },
    async loadMore() {
      if (this.sysInstanceIndex >= Math.ceil(this.sysInstanceTotalPages / 10))
        return;
      this.sysInstanceIndex++;
      this.loading = true;
      // 获取列表数据的方法
      await this.getOrgList();
      this.loading = false;
    },
    // 请求单位列表
    async getOrgList() {
      let { tenantId, userId, roleName } = this.user;
      const res = await reqOrgList({
        tenantId,
        userId,
        roleName,
        orgName: this.orgName,
        parentId: this.searchForm.orgId || "",
        type: "4",
      });
      if (res.code !== 20000) return this.$modal.notifyWarning(res.message);
      this.orgList = res.data.rows;
    },
    // 选择机构
    changeOrgId(instanceId) {
      this.searchForm.orgId = instanceId.id;
      this.handleChange(1);
    },

    // 选择
    handleChange(level) {
      if (level == 1) {
        // 组织机构
        this.orgName = "";
        this.orgList = [];
        this.searchForm.unitId =
          this.searchForm.buildingId =
          this.searchForm.floorId =
          this.searchForm.spaceId =
            "";
        this.orgList = this.buildings = this.floors = [];
        this.selectedValue = this.buildingsName = this.floorsName = "";
        this.sysInstanceIndex = 1;
        this.sysInstanceTotalPages = 10;
        if (this.searchForm.orgId != "") {
          this.getOrgList();
        }
      } else if (level == 2) {
        // 联网单位
        this.searchForm.buildingId =
          this.searchForm.floorId =
          this.searchForm.spaceId =
            "";
        this.buildings = this.floors = [];
        this.buildingsName = this.floorsName = "";
        this.buildingsName2 = "";
        this.sysInstanceIndex2 = 1;
        this.sysInstanceTotalPages2 = 10;
        if (this.searchForm.unitId != "") {
          this.getBuildings();
        }
      } else if (level == 3) {
        // 建筑物
        this.searchForm.floorId = this.searchForm.spaceId = "";
        this.floors = this.spaces = [];
        this.floorsName = this.spacesName = "";
        this.floorsName2 = this.spacesName2 = "";
        this.sysInstanceIndex3 = 1;
        this.sysInstanceTotalPages3 = 10;
        if (this.searchForm.buildingId != "") {
          this.getFloors();
        }
      } else if (level == 4) {
        // 楼层
        this.searchForm.spaceId = "";
        this.spaces = [];
        this.spacesName = "";
        this.spacesName2 = "";
        this.sysInstanceIndex4 = 1;
        this.sysInstanceTotalPages4 = 10;
        if (this.searchForm.floorId != "") {
          this.getSpaces();
        }
      }
    },

    // 清空
    handleClear(level) {
      // if (level == 1) {
      //   // 组织机构
      //   this.orgName = "";
      //   this.orgList = [];
      //   this.searchForm.unitId =
      //     this.searchForm.buildingId =
      //     this.searchForm.floorId =
      //     this.searchForm.spaceId =
      //       "";
      //   this.orgList = this.buildings = this.floors = this.spaces = [];
      //   this.selectedValue = this.buildingsName = this.floorsName = this.spacesName = "";
      //   this.selectedValue2 = this.buildingsName2 = this.floorsName2 = this.spacesName2 = "";
      //   this.sysInstanceIndex = 1;
      //   this.sysInstanceTotalPages = 10;
      //   if (this.searchForm.orgId != "") {
      //     this.getOrgList();
      //   }
      // } else
      if (level == 2) {
        // 联网单位
        this.searchForm.buildingId =
          this.searchForm.floorId =
          this.searchForm.spaceId =
            "";
        this.orgList = this.buildings = this.floors = this.spaces = [];
        this.selectedValue =
          this.buildingsName =
          this.floorsName =
          this.spacesName =
            "";
        this.selectedValue2 =
          this.buildingsName2 =
          this.floorsName2 =
          this.spacesName2 =
            "";
        if (this.user.roleName == "UNIT_ADMIN") {
          this.getOrgList();
          return;
        }
        if (this.searchForm.orgId != "") {
          this.getOrgList();
        }
      } else if (level == 3) {
        // 建筑物
        this.searchForm.buildingId =
          this.searchForm.floorId =
          this.searchForm.spaceId =
            "";
        this.buildings = this.floors = this.spaces = [];
        this.buildingsName = this.floorsName = this.spacesName = "";
        this.buildingsName2 = this.floorsName2 = this.spacesName2 = "";
        this.sysInstanceIndex2 = 1;
        this.sysInstanceTotalPages2 = 10;
        if (this.searchForm.unitId != "") {
          this.getBuildings();
        }
      } else if (level == 4) {
        // 楼层
        this.searchForm.floorId = this.searchForm.spaceId = "";
        this.floors = this.spaces = [];
        this.floorsName = this.spacesName = "";
        this.floorsName2 = this.spacesName2 = "";
        this.sysInstanceIndex3 = 1;
        this.sysInstanceTotalPages3 = 10;
        if (this.searchForm.buildingId != "") {
          this.getFloors();
        }
      } else if (level == 5) {
        // 安装点
        this.searchForm.spaceId = "";
        this.spaces = [];
        this.spacesName = "";
        this.spacesName2 = "";
        this.sysInstanceIndex4 = 1;
        this.sysInstanceTotalPages4 = 10;
        if (this.searchForm.floorId != "") {
          this.getSpaces();
        }
      }
    },
    // 远程搜索
    async remoteBuildings(query) {
      this.buildingsName = query; //保留输入的值
      this.buildingsName2 = query; //保留输入的值
      this.sysInstanceIndex2 = 1;
      this.sysInstanceTotalPages2 = 10;
      this.buildings = [];
      this.getBuildings();
    },
    // 建筑物操作
    async loadBuildings() {
      if (this.sysInstanceIndex2 >= Math.ceil(this.sysInstanceTotalPages2 / 10))
        return;
      this.sysInstanceIndex2++;
      this.loading2 = true;
      // 获取列表数据的方法
      await this.getBuildings();
      this.loading2 = false;
    },
    // 建筑物列表
    async getBuildings() {
      let res = await reqSpaceManager(this.sysInstanceIndex2, 10, {
        userId: this.user.userId,
        roleName: this.user.roleName,
        metaCode: 1,
        orgId: this.selectedValue,
        isStatistics: 0,
        name: this.buildingsName2,
      });
      if (res.code !== 20000) return this.$modal.notifyWarning(res.message);
      this.sysInstanceTotalPages2 = res.data.total;
      this.buildings = [...this.buildings, ...res.data.rows];
    },
    // 选择建筑物
    changeBuildings(e) {
      this.searchForm.buildingId = e;
      this.clearFloors();
    },

    // 楼层

    // 选择建筑物
    changeFloors(e) {
      this.searchForm.floorId = e;
      this.handleChange(4);
    },
    // 清空建筑物
    clearFloors() {
      this.floorsName = "";
      this.floorsName2 = "";
      this.sysInstanceIndex3 = 1;
      this.sysInstanceTotalPages3 = 10;
      this.floors = [];
      this.getFloors();
    },
    // 远程搜索
    async remoteFloors(query) {
      this.floorsName = query; //保留输入的值
      this.floorsName2 = query; //保留输入的值
      this.sysInstanceIndex3 = 1;
      this.sysInstanceTotalPages3 = 10;
      this.floors = [];
      this.getFloors();
    },
    // 建筑物操作
    async loadFloors() {
      if (this.sysInstanceIndex3 >= Math.ceil(this.sysInstanceTotalPages3 / 10))
        return;
      this.sysInstanceIndex3++;
      this.loading3 = true;
      // 获取列表数据的方法
      await this.getFloors();
      this.loading3 = false;
    },
    async getFloors() {
      let res = await reqSpaceManager(this.sysInstanceIndex3, 10, {
        userId: this.user.userId,
        roleName: this.user.roleName,
        metaCode: 2,
        orgId: this.selectedValue,
        isStatistics: 0,
        name: this.floorsName2,
        parentId: this.searchForm.buildingId || "",
      });
      if (res.code !== 20000) return this.$modal.notifyWarning(res.message);
      this.sysInstanceTotalPages3 = res.data.total;
      this.floors = [...this.floors, ...res.data.rows];
    },

    // 安装点
    // 选择安装点
    changeSpaces(e) {
      this.searchForm.spaceId = e;
    },
    // 远程搜索
    async remoteSpaces(query) {
      this.spacesName = query; //保留输入的值
      this.spacesName2 = query; //保留输入的值
      this.sysInstanceIndex4 = 1;
      this.sysInstanceTotalPages4 = 10;
      this.spaces = [];
      this.getSpaces();
    },
    // 建筑物操作
    async loadSpaces() {
      if (this.sysInstanceIndex4 >= Math.ceil(this.sysInstanceTotalPages4 / 10))
        return;
      this.sysInstanceIndex4++;
      this.loading4 = true;
      // 获取列表数据的方法
      await this.getSpaces();
      this.loading4 = false;
    },
    async getSpaces() {
      console.log(this.spacesName, "this.spacesName");
      let res = await reqSpaceManager(this.sysInstanceIndex4, 10, {
        userId: this.user.userId,
        roleName: this.user.roleName,
        metaCode: 3,
        orgId: this.searchForm.unitId,
        isStatistics: 0,
        name: this.spacesName,
        parentId: this.searchForm.floorId || "",
      });
      if (res.code !== 20000) return this.$modal.notifyWarning(res.message);
      this.sysInstanceTotalPages4 = res.data.total;
      this.spaces = [...this.spaces, ...res.data.rows];
    },
  },
};
